{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyTourch Train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import torch\n",
    "from torch.utils.data import DataLoader\n",
    "import torchvision.datasets as dsets\n",
    "import torchvision.transforms as transforms\n",
    "\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import preprocessing\n",
    "\n",
    "batch_size = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "MINST = \"./data/pymnist\"\n",
    "os.makedirs(MINST, exist_ok=True)\n",
    "\n",
    "# MNIST dataset\n",
    "train_dataset = dsets.MNIST(\n",
    "    root = MINST, #选择数据的根目录\n",
    "    train = True, # 选择训练集\n",
    "    transform = None, #不考虑使用任何数据预处理\n",
    "    download = True\n",
    ") # 从网络上download图片\n",
    "\n",
    "test_dataset = dsets.MNIST(\n",
    "    root = MINST, #选择数据的根目录\n",
    "    train = False, # 选择测试集\n",
    "    transform = None, #不考虑使用任何数据预处理\n",
    "    download = True\n",
    ") # 从网络上download图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#加载数据\n",
    "train_loader = torch.utils.data.DataLoader(\n",
    "    dataset = train_dataset, \n",
    "    batch_size = batch_size, \n",
    "    shuffle = True\n",
    ")  # 将数据打乱\n",
    "\n",
    "test_loader = torch.utils.data.DataLoader(\n",
    "    dataset = test_dataset,\n",
    "    batch_size = batch_size,\n",
    "    shuffle = True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train_data torch.Size([60000, 28, 28])\n",
      "train_labels torch.Size([60000])\n",
      "test_data torch.Size([60000, 28, 28])\n",
      "test_labels torch.Size([60000])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:55: UserWarning: train_data has been renamed data\n",
      "  warnings.warn(\"train_data has been renamed data\")\n",
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:45: UserWarning: train_labels has been renamed targets\n",
      "  warnings.warn(\"train_labels has been renamed targets\")\n",
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:60: UserWarning: test_data has been renamed data\n",
      "  warnings.warn(\"test_data has been renamed data\")\n",
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:50: UserWarning: test_labels has been renamed targets\n",
      "  warnings.warn(\"test_labels has been renamed targets\")\n"
     ]
    }
   ],
   "source": [
    "# check the data sizes\n",
    "print(\"train_data: \", train_dataset.train_data.size())\n",
    "print(\"train_labels: \", train_dataset.train_labels.size())\n",
    "print(\"test_data: \", train_dataset.test_data.size())\n",
    "print(\"test_labels: \", train_dataset.test_labels.size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAASIUlEQVR4nO3de4xU9d3H8ffXlVWERYHdkC2l4qO2utJlwcFLUCqaImKU1USDtau2FkwD0ibQPlgS2RhjqD6tqdcGrRHvGpRUU/IIIrQhTSyj7LNiLV2oi0K4LFJx8VJu3+ePHTar7vxmmbv8Pq9kMzPne357vnvChzNzzsz8z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Xpv6jHOzu/10mvTUDe0s9jXdqtqLantOMA43ATZRw3wX6upYi7LdSHNnPATa6+7/cfR/wHDC1BH2UPXf/C7D7S4unAotT9xfT9Y+l6NL0VhbcfZu7v5W63wkcnma8pPsu0FdRlCLsw4EPejzeQnnN9+7AcjN708xmlLqZXgxz922p+9uBYaVsphcZp/Eupi9NM142+y6b6c9zpRN0X3WBu48FLgNmpp6uliXveg1WTtdO+zSNd7H0Ms14t1Luu2ynP89VKcK+FRjR4/E3U8vKgrtvTd3uBJZSflNR7zg8g27qdmeJ++lWTtN49zbNOGWw70o5/Xkpwr4WON3MTjGzSmAa8HIJ+vgKMxuQOnGCmQ0AJlF+U1G/DNyYun8j8McS9vIF5TKNd7ppxinxviv59OfuXvQfYApdZ+Q3AfNL0UOavv4L+L/Uzzul7g14lq6ndfvpOrdxMzAUWAm0Aa8BQ8qotyeBt4FWuoJVW6LeLqDrKXor0JL6mVLqfRfoqyj7TW+XFYmETtCJREJhF4mEwi4SCYVdJBIKu0gkFHaRSCjsIpH4f37f1y0R440kAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "## Draw minst\n",
    "\n",
    "digit = train_loader.dataset.train_data[0]\n",
    "plt.imshow(digit, cmap=plt.cm.binary)\n",
    "plt.text(0, 1, train_loader.dataset.train_labels[0])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### KNN测试分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def kNN_classify(k,dis,X_train,x_train,Y_test):\n",
    "    assert dis == 'E' or dis == 'M', 'dis must E or M，E代表欧拉距离，M代表曼哈顿距离'\n",
    "    num_test = Y_test.shape[0]  #测试样本的数量\n",
    "    labellist = []\n",
    "    '''\n",
    "    使用欧拉公式作为距离度量\n",
    "    '''\n",
    "    if (dis == 'E'):\n",
    "        for i in range(num_test):\n",
    "            # 实现欧拉距离公式\n",
    "            distances = np.sqrt(np.sum(((X_train - np.tile(Y_test[i], (X_train.shape[0], 1))) ** 2), axis=1))\n",
    "            nearest_k = np.argsort(distances)#距离由小到大进行排序，并返回index值\n",
    "            topK = nearest_k[:k]#选取前k个距离\n",
    "            classCount = {}\n",
    "            for i in topK: #统计每个类别的个数\n",
    "                classCount[x_train[i]] = classCount.get(x_train[i],0) + 1\n",
    "            sortedClassCount = sorted(classCount.items(),key=lambda x: x[1],reverse=True)\n",
    "            labellist.append(sortedClassCount[0][0])\n",
    "        return np.array(labellist)\n",
    "\n",
    "    \n",
    "def getXmean(X_train):\n",
    "    X_train = np.reshape(X_train, (X_train.shape[0], -1))  # 将图片从二维展开为一维\n",
    "    mean_image = np.mean(X_train, axis=0)  # 求出训练集所有图片每个像素位置上的平均值\n",
    "    return mean_image\n",
    "\n",
    "def centralized(X_test,mean_image):\n",
    "    X_test = np.reshape(X_test, (X_test.shape[0], -1))  # 将图片从二维展开为一维\n",
    "    X_test = X_test.astype(np.float)\n",
    "    X_test -= mean_image  # 减去均值图像，实现零均值化\n",
    "    return X_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got 277 / 1000 correct => accuracy: 0.277000\n"
     ]
    }
   ],
   "source": [
    "# 直接使用KNN处理\n",
    "X_train = train_loader.dataset.train_data.numpy() #需要转为numpy矩阵\n",
    "X_train = X_train.reshape(X_train.shape[0],28*28)#需要reshape之后才能放入knn分类器\n",
    "y_train = train_loader.dataset.train_labels.numpy()\n",
    "\n",
    "X_test = test_loader.dataset.test_data[:1000].numpy()\n",
    "X_test = X_test.reshape(X_test.shape[0],28*28)\n",
    "\n",
    "y_test = test_loader.dataset.test_labels[:1000].numpy()\n",
    "\n",
    "num_test = y_test.shape[0]\n",
    "y_test_pred = kNN_classify(5, 'E', X_train, y_train, X_test)\n",
    "\n",
    "num_correct = np.sum(y_test_pred == y_test)\n",
    "accuracy = float(num_correct) / num_test\n",
    "print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:55: UserWarning: train_data has been renamed data\n",
      "  warnings.warn(\"train_data has been renamed data\")\n",
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:45: UserWarning: train_labels has been renamed targets\n",
      "  warnings.warn(\"train_labels has been renamed targets\")\n",
      "/mnt/data8/zhangyiming/.env/pyenv/versions/3.8.1/lib/python3.8/site-packages/torchvision/datasets/mnist.py:60: UserWarning: test_data has been renamed data\n",
      "  warnings.warn(\"test_data has been renamed data\")\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got 963 / 1000 correct => accuracy: 0.963000\n"
     ]
    }
   ],
   "source": [
    "# 提前对数据进行预处理，归一化处理\n",
    "X_train = train_loader.dataset.train_data.numpy()\n",
    "mean_image = getXmean(X_train)\n",
    "X_train = centralized(X_train,mean_image)\n",
    "\n",
    "y_train = train_loader.dataset.train_labels.numpy()\n",
    "\n",
    "X_test = test_loader.dataset.test_data[:1000].numpy()\n",
    "X_test = centralized(X_test,mean_image)\n",
    "\n",
    "y_test = test_loader.dataset.test_labels[:1000].numpy()\n",
    "y_test =  np.array(y_test)\n",
    "\n",
    "num_test = y_test.shape[0]\n",
    "y_test_pred = kNN_classify(5, 'E', X_train, y_train, X_test)\n",
    "num_correct = np.sum(y_test_pred == y_test)\n",
    "accuracy = float(num_correct) / num_test\n",
    "print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAARmUlEQVR4nO3dbYxUZZYH8P+hkXfb0EvbgvK2iBKycXHToihZMQOEITE4kZjROHGiLvMB40ycD4v6QRM1ISszEz9sJulZO8OYwckkg5EP7SwsTmLmgyMt4a1htNFgbECakdiNAgLN2Q99Na32Paetp27dss//l3S6+p66VU8X/PtW1annPqKqIKLRb0zZAyCi2mDYiYJg2ImCYNiJgmDYiYIYW8s7a2xs1Obm5lreJVEoJ0+eRH9/vwxXSwq7iKwC8AKABgD/o6obres3Nzdj40bzKkSUYMOGDbm1ip/Gi0gDgP8G8H0ACwHcKyILK709IipWymv2xQAOq+r7qnoewB8ArKnOsIio2lLCfjWAD4f83JNt+woRWScinSLS2d/fn3B3RJSi8DfoVLUNQBsAzJs3b9jP5q5fvx4TJkzAmDFj0NDQwNf1RAVICftRADOH/HxNtq0iTz31FBobGxOGQ0SWlKfxuwDMF5G5IjIOwA8BbKvOsIio2io+sqvqRRF5BMD/YrD11q6qXZXe3nPPPQcAWLFiBZYvX17pzRBRjqTX7KraAaAjdRDPPPMMmpqa0NfXh2effRYzZszAwoXs4hFVU118XLapqQkAcMUVV+Cmm27C4cOHSx4R0ehTetjPnTuHs2fPfnl53759mDVrVsmjIhp9avrZ+OH09fVh06ZNAICBgQEsXboUixYtKndQRKNQ6WFvaWnB888/X/YwiEa90p/GE1FtMOxEQZT+NJ7SiQw7fbnwfcuWcmbkiGdV5pGdKAiGnSgIhp0oCIadKAiGnSgIhp0oCLbeaiC1veXtP2ZM/t9sb9+U2x7J/hav/XXp0qWkunX73r6e72Lrjkd2oiAYdqIgGHaiIBh2oiAYdq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iskNEurPvw66xV9LYnhaRo9ljt0dEVpc0tpki8hcROSgiXSLy02x7qY+dMa6aPG41f80uIg0A3gWwAkAPgF0A7lXVgzUdSA4ROQKgVVVL/wCGiPw7gE8B/E5V/yXb9l8ATqnqxuwP5VRV/c86GdvTAD4texnvbLWi6UOXGQdwF4Afo8THzhjXPajB41bGkX0xgMOq+r6qngfwBwBrShhH3VPVNwCc+trmNQA2Z5c3Y/A/S83ljK0uqOpxVd2dXT4N4Itlxkt97Ixx1UQZYb8awIdDfu5Bfa33rgC2i8jbIrKu7MEMo0VVj2eXPwLQUuZghuEu411LX1tmvG4eu0qWP0/FN+i+aamq/huA7wNYnz1drUs6+BqsnnqnI1rGu1aGWWb8S2U+dpUuf56qjLAfBTBzyM/XZNvqgqoezb73AngF9bcU9YkvVtDNvveWPJ4v1dMy3sMtM446eOzKXP68jLDvAjBfROaKyDgAPwSwrYRxfIOITM7eOIGITAawEvW3FPU2AA9klx8A8GqJY/mKelnGO2+ZcZT82JW+/Lmq1vwLwGoMviP/HoAnyxhDzrj+GcDe7Kur7LEBeBmDT+suYPC9jYcA/BOAnQC6AfwfgKY6GttLAPYD2IfBYE0vaWxLMfgUfR+APdnX6rIfO2NcNXnc+HFZoiD4Bh1REAw7URAMO1EQDDtREAw7URAMO1EQDDtREP8Pqfo53yYoK9UAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "cdata = X_train.reshape(X_train.shape[0],28,28)\n",
    "plt.imshow(cdata[0],cmap=plt.cm.binary)\n",
    "plt.text(0, 1, y_train[0])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cifar10 dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "CIFAR = \"./data/pycifar\"\n",
    "os.makedirs(CIFAR, exist_ok=True)\n",
    "\n",
    "#Cifar10 dataset\n",
    "train_dataset = dsets.CIFAR10(\n",
    "    root = CIFAR, #选择数据的根目录\n",
    "    train = True, # 选择训练集\n",
    "    download = True\n",
    ") # 从网络上download图片\n",
    "\n",
    "test_dataset = dsets.CIFAR10(\n",
    "    root = CIFAR, #选择数据的根目录\n",
    "    train = False, # 选择测试集\n",
    "    download = True\n",
    ") # 从网络上download图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'torch' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-1be2aad5f629>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#加载数据\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m train_loader = torch.utils.data.DataLoader(\n\u001b[0m\u001b[1;32m      4\u001b[0m     \u001b[0mdataset\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrain_dataset\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mbatch_size\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'torch' is not defined"
     ]
    }
   ],
   "source": [
    "#加载数据\n",
    "\n",
    "train_loader = torch.utils.data.DataLoader(\n",
    "    dataset = train_dataset, \n",
    "    batch_size = batch_size, \n",
    "    shuffle = True\n",
    ")  # 将数据打乱\n",
    "\n",
    "test_loader = torch.utils.data.DataLoader(\n",
    "    dataset = test_dataset,\n",
    "    batch_size = batch_size,\n",
    "    shuffle = True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
